
package model;

import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.util.ArrayList;

/**
 *
 * @author Beto
 */
public class Dilatacao {

    public static ArrayList<Polygon> dilatarPoligonos(ArrayList<Polygon> poligonos, double dilatacao) {
        ArrayList<Polygon> poligonosDilatados = new ArrayList<Polygon>();
        
        for (Polygon polygon : poligonos) {

            Polygon polygonAux = new Polygon(polygon.xpoints, polygon.ypoints, polygon.npoints);

            double a = (double)(polygonAux.ypoints[0] - polygonAux.ypoints[1])
                            / (double)(polygonAux.xpoints[1] - polygonAux.xpoints[0]);
            double angle = Math.atan(a);
            
            Point2D.Double baricentro = new Point2D.Double((polygonAux.xpoints[2] + polygonAux.xpoints[0]) / 2,
                                                           (polygonAux.ypoints[3] + polygonAux.ypoints[0]) / 2);

            polygonAux.translate((int)(baricentro.x * -1), (int)(baricentro.y * -1));

            System.out.println("> " + a + ", " + Math.toDegrees(angle));
            
            double X = Math.hypot(polygonAux.xpoints[1] - polygonAux.xpoints[0],
                            polygonAux.ypoints[1] - polygonAux.ypoints[0]);

            double Y = Math.hypot(polygonAux.xpoints[2] - polygonAux.xpoints[1],
                            polygonAux.ypoints[2] - polygonAux.ypoints[1]);

            double scaleX = (X + dilatacao*2) / X;
            double scaleY = (Y + dilatacao*2) / Y;

            AffineTransform transform = new AffineTransform();
            transform.rotate(angle);

            PathIterator iterator = polygonAux.getPathIterator(transform);
            double[] x = new double[6];

            for(int i = 0; i < 4; i++) {
                iterator.currentSegment(x);
                polygonAux.xpoints[i] = (int) x[0];
                polygonAux.ypoints[i] = (int) x[1];
                iterator.next();
            }

            polygonAux.xpoints[0] -= dilatacao;
            polygonAux.xpoints[1] += dilatacao;
            polygonAux.xpoints[2] += dilatacao;
            polygonAux.xpoints[3] -= dilatacao;

            polygonAux.ypoints[0] -= dilatacao;
            polygonAux.ypoints[1] -= dilatacao;
            polygonAux.ypoints[2] += dilatacao;
            polygonAux.ypoints[3] += dilatacao;

            transform = new AffineTransform();
            transform.translate(baricentro.x, baricentro.y);
            transform.rotate(-angle);

            iterator = polygonAux.getPathIterator(transform);
            x = new double[6];

            for(int i = 0; i < 4; i++) {
                iterator.currentSegment(x);
                polygonAux.xpoints[i] = (int) x[0];
                polygonAux.ypoints[i] = (int) x[1];
                iterator.next();
            }

            poligonosDilatados.add(polygonAux);
        }

        return poligonosDilatados;
    }
}
